package com.util;

import java.util.*;

public class User {
    public String userName;
    public int userId;
    public String email;
    public String password;
    public boolean remember;

    public User(){

    }

    public User(int _userId){
        userId = _userId;
        String sql = String.format("select * from user where user_id = %s", userId);
        ArrayList<HashMap<String, String>> res = SQLHelp.selectQuery(sql);
        assert !res.isEmpty();
        HashMap<String, String> inf = res.get(0);
        userName = inf.get("user_name");
        email = inf.get("email");
        password = inf.get("password");
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRemember(boolean remember) {
        this.remember = remember;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public boolean getRemember(){
        return this.remember;
    }

    public static int insertUser(String userName, String email, String password){
        String sql = String.format("insert into user(user_name, email, password) select '%s','%s','%s' from dual where not exists (select user_name, email, password from user where user_name = '%s')",userName,email,password,userName);
        return SQLHelp.update(sql);
    }

    public static boolean checkPassword(String user, String password){
//        Regex reg = new Regex("/^([a-zA-Z\\d][\\w-]{2,})@(\\w{2,})\\.([a-z]{2,})(\\.[a-z]{2,})?$/");
        //判断用邮箱登录
        String sql;
        if (user.matches("/^([a-zA-Z\\d][\\w-]{2,})@(\\w{2,})\\.([a-z]{2,})(\\.[a-z]{2,})?$/")){
            sql = String.format("select password from user where email = '%s'", user);
        }else {
            sql = String.format("select password from user where user_name = '%s'", user);
        }
        List<HashMap<String, String>> res = SQLHelp.selectQuery(sql);
        //判断密码是否正确
        return !res.isEmpty() && !res.get(0).get("password").isEmpty()
                && Objects.equals(res.get(0).get("password"), password);
    }

    public Integer getUserId(){
        String sql = String.format("select user_id from user where user_name='%s'",getUserName());
        ArrayList<HashMap<String,String>> res = SQLHelp.selectQuery(sql);
        if(res.isEmpty()){
            return 0;
        }
        return Integer.parseInt(res.get(0).get("user_id"));
    }


    public ArrayList<HashMap<String, String>> getPanList(){
        String sql = String.format("select * from baiduoauthtable where user_id = %s", getUserId());
        ArrayList<HashMap<String, String>> res = SQLHelp.selectQuery(sql);
        return res;
    }

    public static boolean isExistName(String userName){
        String sql = String.format("select user_id from user where user_name='%s'",userName);
        ArrayList<HashMap<String,String>> res = SQLHelp.selectQuery(sql);
        return !res.isEmpty();
    }

    public static ArrayList<HashMap<String, String>> folderList(String userId,String folderName){
        String sqlFormat;
        if (folderName.equals("")){
            sqlFormat = "select * from folder where user_id = %s";
        }
        else {
            sqlFormat = "select * from folder where user_id = %s and folder_name like '%%%s%%'";
        }
        String sql = String.format(sqlFormat, userId,folderName);
        ArrayList<HashMap<String, String>> result = SQLHelp.selectQuery(sql);
        return result;
    }

    public static void changeStar(String folderKey,String star){
        String sql=String.format("update folder set star = %s where folder_key = '%s'",star,folderKey);
        SQLHelp.update(sql);
    }

    public static void changeOpen(String folderKey,String open){
        String sql=String.format("update folder set folder_open = %s where folder_key = '%s'",open,folderKey);
        SQLHelp.update(sql);
    }

    public static String getEmailByName(String userName){
        String sql=String.format("select email from user where user_name = '%s'",userName);
        if (SQLHelp.selectQuery(sql).size() == 0){
            return "";
        }else {
            return SQLHelp.selectQuery(sql).get(0).get("email");
        }
    }

    public static void changePassword(String userName,String password){
        String sql = String.format("update user set password = '%s' where user_name = '%s'",password,userName);
        SQLHelp.update(sql);
    }
}
